//
//  PZLLocationViewController.m
//  Various
//
//  Created by dllo on 16/4/1.
//  Copyright © 2016年 Harword. All rights reserved.
//

#import "PZLLocationViewController.h"
#import "HLNetWorkTool.h"
@interface PZLLocationViewController ()<UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *titleArr;
@property (nonatomic, strong)NSMutableArray *hotCitiesArr;
@property (nonatomic, strong)NSMutableArray *dicArr;
@property (nonatomic, strong)NSArray *array;

@end

@implementation PZLLocationViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatSubView];
    [self creatCell];
    
}



- (void)creatCell{
    
    
    
    self.array = @[@"热", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
    
    
    [HLNetWorkTool getWithURL:@"http://wl.myzaker.com/?_appid=AndroidPhone&_v=6.5.1&_version=6.53&c=city_list&lat=38.889653&lng=121.551125" Parameter:nil HttpHeader:nil ResponseType:ResponseTypeJSON Progress:^(NSProgress *progress) {
        
        
    } Success:^(id result) {
        self.titleArr = [NSMutableArray array];
        self.hotCitiesArr = [NSMutableArray array];
        self.dicArr = [NSMutableArray array];
        NSMutableArray *modelArr = [NSMutableArray array];
        
        NSDictionary *dicData = [result objectForKey:@"data"];
        
        //hot_cities
        NSArray *hotArr = [dicData objectForKey:@"hot_cities"];
        for (NSDictionary *dic in hotArr) {
            PZLLocationModel *model = [PZLLocationModel baseModelWithDict:dic];
            [modelArr addObject:model];
        }

        //cities
        NSArray *citiesArr = [dicData objectForKey:@"cities"];
        for (NSDictionary *dic in citiesArr) {
            PZLLocationModel *model = [PZLLocationModel baseModelWithDict:dic];
            [modelArr addObject:model];
        
        }
        
        
        for (int i = 0; i<self.array.count; i++) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            NSMutableArray *arr = [NSMutableArray array];
            for (PZLLocationModel *temp in modelArr) {
                
//                 NSLog(@"-------/%@", temp.letter);
                if ([temp.letter isEqualToString:self.array[i]]) {
                   
                    [arr addObject:temp];
                }
                
            }
            
            if (arr.count != 0) {
            [dic setObject:arr forKey:self.array[i]];
            [self.titleArr addObject:self.array[i]];
            [self.dicArr addObject:dic];
            }
          
        }
        
        [self.tableView reloadData];
        
    } Failure:^(NSError *error) {
        
        
    }];
}













- (void)creatSubView{
 
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, sWEIGHT * 414, sHEIGHT * 736) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  
    
    return self.dicArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
     NSArray *arrCount = [self.dicArr[section] objectForKey:self.titleArr[section]];
    
    
    return arrCount.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID = @"text";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (self) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    
    NSArray *arr = [self.dicArr[indexPath.section] objectForKey:self.titleArr[indexPath.section]];
    PZLLocationModel *model = arr[indexPath.row];
    
    
    cell.textLabel.text = model.city_name;
    return cell;
    
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.titleArr[section];
}


- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.titleArr;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *arrayModel = [self.dicArr[indexPath.section] objectForKey:self.titleArr[indexPath.section]];
    
    PZLLocationModel *model = arrayModel[indexPath.row];
    
    [self.delegate text:model];
}


















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}


@end
